
#import "IPFlyPanel.h"

@interface View (IPFlyHack)

- getIconifyButton;

@end


@interface IPFlyPanel (IPFlyPanelPrivateMethods)

- _syncIconifyButton;		

@end

static const char *PANELPREFIX = "IPFLYPANEL";

@implementation IPFlyPanel


- initContent:(const NXRect *)contentRect
        style:(int)aStyle
      backing:(int)bufferingType
   buttonMask:(int)mask
        defer:(BOOL)flag
{
	[super initContent:contentRect
	             style:aStyle
               backing:bufferingType
	        buttonMask:mask
 	             defer:flag];
				 
	if([_borderView respondsTo:@selector(getIconifyButton)] &&
	     [_borderView getIconifyButton] != nil){
		[[_borderView getIconifyButton] setType:NX_TOGGLE];
		[[_borderView getIconifyButton] setIcon:"fly.tiff"];
		[[_borderView getIconifyButton] setAltIcon:"flyH.tiff"];
	}
	_floatingName = NULL;
	return self;
}

- free
{
	NX_FREE(_floatingName);
	return [super free];
}

- _syncIconifyButton
{
	if ( [_borderView respondsTo:@selector(getIconifyButton)] &&
	     [_borderView getIconifyButton] != nil )
	{
		[[_borderView getIconifyButton] setState:[self isFloatingPanel]];
		[[_borderView getIconifyButton] update];
	}
	return self;
}

- miniaturize:sender
{
	[super setFloatingPanel:![self isFloatingPanel]];
	if(_floatingName != NULL)
		[self saveFloatingUsingName:_floatingName];
	return self;
}

- setFloatingPanel:(BOOL)flag
{
	[super setFloatingPanel:flag];
	[self _syncIconifyButton];
	if(_floatingName != NULL)
		[self saveFloatingUsingName:_floatingName];
	return self;
}

- (BOOL)setFloatingAutosaveName:(const char *)name
{
	if(name != NULL){
		if(_floatingName)
			_floatingName = (char *)NXZoneRealloc([self zone], _floatingName, strlen(name) + 1);
		else
			_floatingName = (char *)NXZoneMalloc([self zone], strlen(name) + 1);
		strcpy(_floatingName, name);
		if(![self setFloatingUsingName:_floatingName])
			[self saveFloatingUsingName:_floatingName];
	} else {
		if(_floatingName)
			NX_FREE(_floatingName);
		_floatingName = NULL;
	}	
	return YES;
}


- (BOOL)setFloatingAutosaveLikeFrameName
{
	return [self setFloatingAutosaveName:[self frameAutosaveName]];
}


- (const char *)floatingAutosaveName
{
	return _floatingName;
}

- (void)saveFloatingUsingName:(const char *)name
{
	char *fname;

	if(name != NULL){
		fname = (char *)NXZoneMalloc([self zone], strlen(name) + strlen(PANELPREFIX) + 1);
		strcpy(fname, PANELPREFIX);
		strcat(fname, name);
		if([self isFloatingPanel])
			NXWriteDefault([NXApp appName], fname, "1");
		else
			NXWriteDefault([NXApp appName], fname, "0");
		NX_FREE(fname);
	}
}


- (BOOL)setFloatingUsingName:(const char *)name
{
	const char	*state;
	char *fname;
	
	if(name != NULL){
		fname = (char *)NXZoneMalloc([self zone], strlen(name) + strlen(PANELPREFIX) + 1);
		strcpy(fname, PANELPREFIX);
		strcat(fname, name);
		state = NXGetDefaultValue([NXApp appName], fname);
		NX_FREE(fname);
		if(state != NULL){
			[super setFloatingPanel:atoi( state )];
			[self _syncIconifyButton];
			return YES;
		}
	}
	return NO;
}

@end
